﻿#region 命名空间

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Specialized;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.BLL;
using WinnerCMS.Data;

#endregion

namespace WinnerCMS.BLL.PageFactory
{
    /// <summary>
    /// 文件路径处理类
    /// </summary>
    public static class StaticFilePath
    {
        #region 内容页URL

        /// <summary>
        /// 内容页URL
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static string ContentShowUrl(DataRow dr)
        {
            var model = new Core
            {
                Id = (int) dr["ID"],
                CategoryId = (int) dr["CategoryId"],
                UpdateTime = (DateTime) dr["UpdateTime"],
                TemplateFileName = dr["TemplateFileName"].ToString()
            };

            return ContentShowUrl(model, 1);
        }

        /// <summary>
        /// 内容页URL
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string ContentShowUrl(dynamic model)
        {
            return ContentShowUrl(model, 1);
        }

        /// <summary>
        /// 内容页URL，实体必备ID\CategoryId\UpdateTime
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public static string ContentShowUrl(dynamic model, int pageNo)
        {
            if (model == null)
            {
                return string.Empty;
            }
#if DEBUG
            if (model.Id == 0)
                throw new FormatException("输入的实体类不能少了model.ID");
            if (model.CategoryId == 0)
                throw new FormatException("输入的实体类不能少了model.CategoryId");
            if (model.UpdateTime == DateTime.MinValue)
                throw new FormatException("输入的实体类不能少了model.UpdateTime");
            if (string.IsNullOrEmpty(model.TemplateFileName))
                throw new ArgumentException("没有模板参数");
#endif
            if (model == null)
                return string.Empty;

            Category cmodel = CategoryManage.GetInfo(model.CategoryId);

            string pageName =
                model.TemplateFileName.Substring(model.TemplateFileName.LastIndexOfAny(new[] {'/', '\\'}) + 1);
            pageName = pageName.Substring(0, pageName.LastIndexOf(".aspx", StringComparison.Ordinal));


            var outPut = new StringBuilder();
            outPut.Append(cmodel.ContentUrlFormat);
            outPut.Replace("{PageName}", pageName);
            outPut.Replace("{ID}", model.Id.ToString());
            outPut.Replace("{PageNo}", pageNo.ToString());
            if (cmodel.UrlStyle == 2)
            {
                outPut.Replace("{yyyyMM}", model.UpdateTime.ToString("yyyyMM"));
            }
            if (cmodel.UrlStyle == 3)
            {
                outPut.Replace("{yyyyMM/dd}", model.UpdateTime.ToString("yyyyMM/dd"));
            }
            return outPut.ToString();
        }

        #endregion

        #region 栏目页URL

        /// <summary>
        /// 栏目页URL
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static string CategoryUrl(int categoryId)
        {
            Category model = CategoryManage.GetInfo(categoryId);
            return CategoryUrl(model, 1);
        }

        /// <summary>
        /// 栏目页URL
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static string CategoryUrl(DataRow dr)
        {
            Category model = new Category
            {
                Id = (int) dr["ID"],
                ModelId = (byte) dr["ModelId"],
                CategoryName = dr["CategoryName"].ToString(),
                CategoryNameEN = dr["CategoryName_EN"].ToString()
            };


            return CategoryUrl(model, 1);
        }

        /// <summary>
        /// 栏目页URL
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string CategoryUrl(Category model)
        {
            return CategoryUrl(model, 1);
        }

        /// <summary>
        /// 栏目页URL
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public static string CategoryUrl(Category model, int pageNo)
        {
            if (model == null)
            {
                return string.Empty;
            }
#if DEBUG
            if (model.Id == 0)
                throw new FormatException("输入的实体类不能少了model.ID");
            if (model.CategoryName == null)
                throw new FormatException("输入的实体类不能少了model.CategoryName");
            if (model.CategoryNameEN == null)
                throw new FormatException("输入的实体类不能少了model.CategoryName_EN");
            if (string.IsNullOrEmpty(model.TemplateFileName))
                throw new ArgumentException("没有模板参数");
#endif

            Category cmodel;
            if (model.Model == null) //如果不是完整的
            {
                cmodel = CategoryManage.GetInfo(model.Id);
            }
            else
            {
                cmodel = model;
            }
            if (cmodel == null)
            {
                return string.Empty;
            }
            if (string.IsNullOrEmpty(cmodel.LinkUrl) == false)
            {
                return cmodel.LinkUrl;
            }

            if (pageNo > cmodel.Model.MaxCategoryPageNo && cmodel.Model.MaxCategoryPageNo > 0)
            {
                cmodel.CategoryUrlStyle = 0; //如果是启用了节约硬盘的生成HTML方案就在超过设置的部分使用动态页
            }

            StringBuilder outPut = new StringBuilder();
            outPut.Append(model.CategoryUrlFormat);
            outPut.Replace("{PageNo}", pageNo.ToString());

            return outPut.ToString();
        }

        #endregion

        #region 专题页

        public static string SpecialUrl(DataRow dr)
        {
            Special model = new Special
            {
                Id = (int) dr["ID"],
                SpecialName = dr["SpecialName"].ToString(),
                SpecialNameEN = dr["SpecialName_EN"].ToString()
            };

            return SpecialUrl(model, false, 1);
        }

        /// <summary>
        /// 专题页
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static string SpecialUrl(Special model)
        {
            return SpecialUrl(model, false, 1);
        }

        /// <summary>
        /// 专题页
        /// </summary>
        /// <param name="model"></param>
        /// <param name="forSaveHtml">强制返回html路径</param>
        /// <param name="pageNo"></param>
        /// <returns></returns>
        public static string SpecialUrl(Special model, bool forSaveHtml, int pageNo)
        {
#if DEBUG
            if (model.Id == 0)
                throw new FormatException("输入的实体类不能少了model.ID");
            if (model.SpecialName == null)
                throw new FormatException("输入的实体类不能少了model.SpecialName");
            if (model.SpecialNameEN == null)
                throw new FormatException("输入的实体类不能少了model.SpecialName_EN");

#endif

            //SpecialInfo cmodel = new SpecialManage().GetInfo(model.ID.ToString());

            //if (cmodel.ModelInfo.EnableStatic == 0)
            //{
            //    cmodel.SpecialURLStyle = 0;

            //}

            //string OutPut = null;
            //switch (cmodel.SpecialURLStyle)
            //{
            //    case 1:
            //        if (cmodel.DomainName.Length == 0 || ForSaveHTML == true)
            //            OutPut = ModelHtmlPath(model.ModelId) + "/Special/" + model.ID + "/" + cmodel.SpecialFilePrefix + model.ID + "_" + PageNo + cmodel.SpecialFilePostfix;
            //        else
            //            OutPut = cmodel.DomainName + "/Special/" + model.ID + "/" + cmodel.SpecialFilePrefix + model.ID + "_" + PageNo + cmodel.SpecialFilePostfix;
            //        break;
            //    case 2:
            //        string strPinYinCategoryName = Chinese2Spell.MakeSpellCode(model.SpecialName, SpellOptions.EnableUnicodeLetter);
            //        if (cmodel.DomainName.Length == 0 || ForSaveHTML == true)
            //            OutPut = ModelHtmlPath(model.ModelId) + "/Special/" + strPinYinCategoryName + "/" + cmodel.SpecialFilePrefix + model.ID + "_" + PageNo + cmodel.SpecialFilePostfix;

            //        else
            //            OutPut = cmodel.DomainName + "/Special/" + strPinYinCategoryName + "/" + cmodel.SpecialFilePrefix + model.ID + "_" + PageNo + cmodel.SpecialFilePostfix;
            //        break;
            //    default:
            //        if (ForSaveHTML == true)
            //            OutPut = ModelHtmlPath(model.ModelId) + "/Special/" + model.ID + "/" + model.ID + "_" + PageNo + cmodel.SpecialFilePostfix;
            //        else
            //        {
            //            string url = string.Format("{0}/Special.aspx?ModelId={1}&SpecialID={2}&PageNo={3}", ModelHtmlPath(model.ModelId), model.ModelId, model.ID, PageNo);

            //            OutPut = UrlRewrite(url, cmodel.UrlRewrite);
            //        }
            //        break;
            //}

            //return OutPut;

            return "";
        }

        #endregion

        #region 留言簿URL

        /// <summary>
        /// 留言簿URL
        /// </summary>
        /// <returns></returns>
        public static string GuestBookUrl()
        {
            return string.Concat(SysPath, "/GuestBook.aspx");
        }

        #endregion

        #region 系统路径

        /// <summary>
        /// 系统路径
        /// </summary>
        /// <returns></returns>
        public static string SysPath
        {
            get
            {
                if (string.IsNullOrEmpty(WebConfig.VirtualPath))
                    return string.Empty;
                return string.Concat("/", WebConfig.VirtualPath);
            }
        }

        #endregion

        #region 网页CSS路径

        /// <summary>
        /// 网页CSS路径
        /// </summary>
        public static string StylePath
        {
            get
            {
                Template model = TemplatesManage.GetUseing;

                return string.Concat(SysPath, "/Templates/", model.TemplatePath, "/", model.Style);
                ;
            }
        }

        #endregion

        #region 模型的目录

        /// <summary>
        /// 模型的目录
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public static string ModelHtmlPath(byte modelId)
        {
            return ModelHtmlPath(ModelManage.GetInfo(modelId));
        }

        /// <summary>
        /// 模型的目录
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        public static string ModelHtmlPath(Model.Model mm)
        {
            string outPut = string.Concat(SysPath, mm.HtmlSavePath);

            if (outPut.EndsWith("/") || outPut.EndsWith(@"\"))
            {
                outPut = outPut.Substring(0, outPut.Length - 1);
            }

            return outPut;
        }

        #endregion

        #region 模板目录

        /// <summary>
        /// 模板目录
        /// </summary>
        public static string TemplatePath
        {
            get { return "/Templates/" + TemplatesManage.GetUseing.TemplatePath; }
        }

        #endregion

        #region 首页

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public static string IndexUrl()
        {
            return IndexUrl(false);
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="forSaveHtml"></param>
        /// <returns></returns>
        public static string IndexUrl(bool forSaveHtml)
        {
            if (forSaveHtml)
            {
                return string.Concat(SysPath, "/Index.html");
            }
            return string.Concat(SysPath, "/");
        }

        #endregion

        #region  专题首页

        /// <summary>
        /// 专题首页
        /// </summary>
        /// <param name="modelId"></param>
        /// <returns></returns>
        public static string SpecialIndexUrl(byte modelId)
        {
            return SpecialIndexUrl(modelId, false);
        }

        /// <summary>
        /// 专题首页
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="forSaveHtml">强制返回html路径</param>
        /// <returns></returns>
        public static string SpecialIndexUrl(byte modelId, bool forSaveHtml)
        {
            if (forSaveHtml)
            {
                return string.Concat(ModelHtmlPath(modelId), "/SpecialIndex.html");
            }
            return string.Concat(ModelHtmlPath(modelId), "/SpecialIndex.aspx");
        }

        #endregion
    }
}